package pass;

import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class CopyFile {
	public static boolean CopyOneFile(String from, String to, boolean rewrite) {
		java.io.File filefrom = new java.io.File(from);
		java.io.File fileto = new java.io.File(to);
		if (!filefrom.exists()) {
			System.out.println("文件不存在");
			return false;
		}
		if (!filefrom.isFile()) {
			System.out.println("不能够复制文件夹");
			return false;
		}
		if (!filefrom.canRead()) {
			System.out.println("不能够读取需要复制的文件");
			return false;
		}
		if (!fileto.getParentFile().exists()) {
			fileto.getParentFile().mkdirs();
		}
		if (fileto.exists() && rewrite) {
			fileto.delete();
		}

		try {
			java.io.FileInputStream fosfrom = new java.io.FileInputStream(
					filefrom);
			InputStreamReader iReader = new InputStreamReader(fosfrom);
			java.io.FileOutputStream fosto = new FileOutputStream(fileto);
			OutputStreamWriter oWriter = new OutputStreamWriter(fosto, "utf-8");
			char[] ch = new char[1024];
			int c;
			while ((c = iReader.read(ch)) > 0) {
				oWriter.write(ch);
			}
			iReader.close();
			oWriter.close();
			fosfrom.close();
			fosto.close();
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

	}

	public static void main(String[] args) {
		CopyOneFile("D:/oracle_ecms.txt", "D:/a.txt", true);
	}
}
